@echo off
Set IC_TESTS_SOURCE_FOLDER=%~dp0test
Set IC_TESTS_OUTPUT_FOLDER=%~dp0TestsResults
Set IC_RUN_STRING=java IC.Compiler
Set BUILD_LOG_FILE=%~dp0Build.log
Set AUTO_TEST_LOG=%IC_TESTS_SOURCE_FOLDER%\AutoTest.log

pushd %~dp0

Echo **** %TIME% Compiling, Please wait..
Echo **** %TIME% Starting new compilation session > %BUILD_LOG_FILE%

Echo ** %TIME% Running "ant clean"..
Echo ** %TIME%  Running "ant clean".. >> %BUILD_LOG_FILE%
cmd /k "ant clean & exit" 1>> %BUILD_LOG_FILE%
IF %ERRORLEVEL% NEQ 0 (goto :errorfound) else (echo Finished.)

Echo ** %TIME% Running "ant build"..
Echo ** %TIME% Running "ant build".. >> %BUILD_LOG_FILE%
cmd /k "ant build & exit" 1>> %BUILD_LOG_FILE%
IF %ERRORLEVEL% NEQ 0 (goto :errorfound) else (echo Finished.)

Echo ** %TIME% Running "ant scanner"..
Echo ** %TIME% Running "ant scanner".. >> %BUILD_LOG_FILE%
cmd /k "ant scanner & exit" 1>> %BUILD_LOG_FILE%
IF %ERRORLEVEL% NEQ 0 (goto :errorfound) else (echo Finished.)

Echo ** %TIME% Running "ant parser"..
Echo ** %TIME% Running "ant parser".. >> %BUILD_LOG_FILE%
cmd /k "ant parser & exit" 1>> %BUILD_LOG_FILE%
IF %ERRORLEVEL% NEQ 0 (goto :errorfound) else (echo Finished.)

Echo ** %TIME% Running "ant libparser"..
Echo ** %TIME% Running "ant libparser".. >> %BUILD_LOG_FILE%
cmd /k "ant libparser & exit" 1>> %BUILD_LOG_FILE%
IF %ERRORLEVEL% NEQ 0 (goto :errorfound) else (echo Finished.)

:runtest
mkdir %IC_TESTS_OUTPUT_FOLDER%

cd /D %~dp0classes

Echo ** Deleting old test results..
del %IC_TESTS_SOURCE_FOLDER%\*.AutoTestOutput.txt /s /q 1>nul

Echo **** %TIME% Starting test session for files in %IC_TESTS_SOURCE_FOLDER%
Echo **** %TIME% Starting test session for files in %IC_TESTS_SOURCE_FOLDER% > %AUTO_TEST_LOG% 
for /f "delims=|" %%F in ('dir /b /s %~dp0test\*.ic') do (
	Echo.
	Echo --Running test for "%%F"..
	%IC_RUN_STRING% "%%F" -L"%~dp0test\libic.sig" -dump-symtab > "%%F.AutoTestOutput.txt" 2>&1
	if %errorlevel% equ 0 (findstr "error" "%%F.AutoTestOutput.txt" && (Echo Error Found - %%F >> %AUTO_TEST_LOG%) || (findstr "Exception" "%%F.AutoTestOutput.txt" && (Echo Exception Found - %%F >> %AUTO_TEST_LOG%) || (echo Success - %%F >> %AUTO_TEST_LOG%))
		Echo Done.)	else (Echo ----ERROR: Test finished with error %errorlevel%
		Echo *** Error - %%F >> %AUTO_TEST_LOG%)
)
Echo.
Echo Opening folder..
popd
start notepad %AUTO_TEST_LOG%
goto end

:errorfound
Echo *********** Build Error. Check %BUILD_LOG_FILE% for details.
popd
start notepad %BUILD_LOG_FILE%
goto end

:end
timeout 15
